package com.timeshare.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.timeshare.common.Result;
import com.timeshare.dto.NoticeRequest;
import com.timeshare.entity.Notice;
import com.timeshare.entity.User;
import com.timeshare.service.NoticeService;
import com.timeshare.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * 公告控制器
 */
@Tag(name = "公告管理")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    
    private final NoticeService noticeService;
    private final UserService userService;
    
    public NoticeController(NoticeService noticeService, UserService userService) {
        this.noticeService = noticeService;
        this.userService = userService;
    }
    
    /**
     * 发布公告（管理员）
     */
    @Operation(summary = "发布公告")
    @PostMapping("/publish")
    public Result<String> publishNotice(Authentication authentication, @Valid @RequestBody NoticeRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            
            // 验证管理员权限
            User user = userService.getById(userId);
            if (user == null || user.getRole() != 1) {
                return Result.error("无权发布公告");
            }
            
            noticeService.publishNotice(userId, request);
            return Result.success("发布成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取公告列表
     */
    @Operation(summary = "获取公告列表")
    @GetMapping("/list")
    public Result<Page<Notice>> getNoticeList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<Notice> notices = noticeService.getNoticeList(page, size);
            return Result.success(notices);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取公告详情
     */
    @Operation(summary = "获取公告详情")
    @GetMapping("/{id}")
    public Result<Notice> getNoticeDetail(@PathVariable Long id) {
        try {
            Notice notice = noticeService.getNoticeDetail(id);
            return Result.success(notice);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新公告（管理员）
     */
    @Operation(summary = "更新公告")
    @PutMapping("/{id}")
    public Result<String> updateNotice(
            Authentication authentication,
            @PathVariable Long id,
            @Valid @RequestBody NoticeRequest request) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            
            // 验证管理员权限
            User user = userService.getById(userId);
            if (user == null || user.getRole() != 1) {
                return Result.error("无权修改公告");
            }
            
            noticeService.updateNotice(id, request);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除公告（管理员）
     */
    @Operation(summary = "删除公告")
    @DeleteMapping("/{id}")
    public Result<String> deleteNotice(Authentication authentication, @PathVariable Long id) {
        try {
            Long userId = (Long) authentication.getPrincipal();
            
            // 验证管理员权限
            User user = userService.getById(userId);
            if (user == null || user.getRole() != 1) {
                return Result.error("无权删除公告");
            }
            
            noticeService.deleteNotice(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

